home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93c.txt / 000011_icon-group-sender _Sat Jul 10 17:08:09 1993.msg < prev    next >
Internet Message Format  |  1994-02-02  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 14 Jul 1993 09:46:07 MST
  2. Date: 10 Jul 93 17:08:09 GMT
  3. From: cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uchinews!ellis!goer@ucbvax.Berkeley.EDU  (Richard L. Goerwitz)
  4. Organization: University of Chicago
  5. Subject: string replacement routine
  6. Message-Id: <1993Jul10.170809.17122@midway.uchicago.edu>
  7. Sender: icon-group-request@cs.arizona.edu
  8. To: icon-group@cs.arizona.edu
  9. Status: R
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. While I'm working here online, I thought I'd post a utility program
  13. I have around that might be of use to some folks.
  14.  
  15. -Richard
  16.  
  17. ############################################################################
  18. #
  19. #    Name:     mapstrs.icn
  20. #
  21. #    Title:     map() for strings
  22. #
  23. #    Author:     Richard L. Goerwitz
  24. #
  25. #    Version: 1.1
  26. #
  27. ############################################################################
  28. #
  29. #  Mapstrs(s, l1, l2) works like map(), except that instead of taking
  30. #  ordered character sequences (strings) as arguments 2 and 3, it
  31. #  takes ordered string sequences (lists).
  32. #
  33. #  Suppose, for example, you wanted to bowdlerize a string by
  34. #  replacing the words "hell" and "shit" with "heck" and "shoot."  You
  35. #  would call mapstrs as follows:
  36. #
  37. #      mapstrs(s, ["hell", "shit"], ["heck", "shoot"])
  38. #
  39. #  In order to achieve reasonable speed, mapstrs creates a lot of
  40. #  static structures, and uses some extra storage.  If you want to
  41. #  replace one string with another, it is overkill.  Just use the IPL
  42. #  replace() routine (in strings.icn).
  43. #
  44. #  If l2 is longer than l1, extra members in l2 are ignored.  If l1 is
  45. #  longer, however, strings in l1 that have no correspondent in l2 are
  46. #  simply deleted.  Mapstr uses a longest-possible-match approach, so
  47. #  that replacing ["hellish", "hell"] with ["heckish", "heck"] will
  48. #  work as one would expect.
  49. #
  50. ############################################################################
  51. #
  52. #  Links: longstr
  53. #
  54. ############################################################################
  55.  
  56. link longstr
  57.  
  58. procedure mapstrs(s, l1, l2)
  59.  
  60.     local i, s2
  61.     static cs, tbl, last_l1, last_l2
  62.  
  63.     if /l1 | *l1 = 0 then return s
  64.  
  65.     if not (last_l1 === l1, last_l2 === l2) then {
  66.     cs := ''
  67.     every cs ++:= (!l1)[1]
  68.     tbl := table()
  69.     every i := 1 to *l1 do
  70.         insert(tbl, l1[i], (\l2)[i] | "")
  71.     }
  72.  
  73.     s2 := ""
  74.     s ? {
  75.     while s2 ||:= tab(upto(cs)) do
  76.         s2 ||:= tbl[tab(longstr(l1))] | move(1)
  77.     s2 ||:= tab(0)
  78.     }
  79.  
  80.     return s2
  81.  
  82. end
  83. -- 
  84.  
  85.    -Richard L. Goerwitz              goer%midway@uchicago.bitnet
  86.    goer@midway.uchicago.edu          rutgers!oddjob!ellis!goer
  87.